package control.panel
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.utils.ByteArray;
	
	import global.GameState;
	
	import model.load.ImgUploadMessage;
	
	import utils.adobe.JPGEncoder;
	
	import view.panel.feed.FeedPanel;
	import view.components.RemoteHtmlImg;

	public class FeedPanelControl extends PanelControl
	{
		private var jpgEncoder:JPGEncoder;
		private var canvasData:BitmapData;
		private var outByteArray:ByteArray;
		private var imgUploadMsg:ImgUploadMessage;
		
		public function FeedPanelControl()
		{
			initPanel();
			initEncoder();
		}
		
		private function initPanel():void
		{
			_view.feedPanel = new FeedPanel(this);
			_view.feedPanel.feedText.addEventListener(Event.CHANGE,onFeedChange);
		}
		
		private function onFeedChange(event:Event):void
		{
			_view.feedPanel.hint("");
		}
		
		private function initEncoder():void
		{
			jpgEncoder = new JPGEncoder();
			canvasData = new BitmapData(GameState.GAME_WIDTH * .8,GameState.GAME_HEIGHT * .8,true);
		}
		
		private function copyScene():void
		{
			var matrix:Matrix = new Matrix();
			matrix.scale(.8,.8)
				
			RemoteHtmlImg.show(false);	
			
			canvasData.draw(_view.scene,matrix);
			RemoteHtmlImg.show(true);	
			_view.feedPanel.showCanvas(canvasData);
			
		}
		
		public function upload():void
		{
			if (_view.feedPanel.content == "")
			{
				_view.feedPanel.hint("请输入反馈内容.");
				return;
			}
			else if (_view.feedPanel.content.length < 8)
			{
				_view.feedPanel.hint("反馈内容长度(8 - 256)");
				return;
			}
			
			outByteArray = jpgEncoder.encode(canvasData);
			outByteArray = _model.netRemote.getFeed(GameState.snsKey,_view.feedPanel.content,_view.feedPanel.type,outByteArray,GameState.snsId);
			outByteArray.compress();
			imgUploadMsg = new ImgUploadMessage(onUploadBack);
			imgUploadMsg.urlvariable = outByteArray;
			trace(outByteArray.length);
			imgUploadMsg.url = GameState.getServerLink() + "24";
			trace(imgUploadMsg.url);
			imgUploadMsg.send();
			_view.feedPanel.enableFeedBut(false);
			_view.feedPanel.hint("上传中请稍等...");
			_view.feedPanel.enableFeedText = false;
		}
		
		private function onUploadBack(data:String):void
		{
			trace("upload",data);
			_view.feedPanel.enableFeedBut(true);
			_view.feedPanel.close();
		}
		
		public function open():void
		{
			if (_view.feedPanel.parent)
			{
				return;
			}
			
			copyScene();
			_view.feedPanel.enableFeedText = true;
			_view.feedPanel.open();
			_view.stage.focus = _view.feedPanel.feedText;
			
		}
		
		public function close():void
		{
			_view.feedPanel.enableFeedBut(true);
			_view.feedPanel.hint("");
			_view.feedPanel.feedText.text = "";
		}
	}
}